<template>
  <nav class="tab-bar">
    <div 
      v-for="item in tabItems" 
      :key="item.path"
      class="tab-item"
      :class="{ active: activeTab === item.path }"
      @click="navigate(item.path)"
    >
      <div class="tab-icon">{{ item.icon }}</div>
      <div class="tab-text">{{ item.text }}</div>
    </div>
  </nav>
</template>

<script>
export default {
  name: 'TabBar',
  data() {
    return {
      tabItems: [
        { path: '/overview', text: '概况', icon: '📊' },
        { path: '/event-manage', text: '事件', icon: '📝' },
        { path: '/home', text: '首页', icon: '🏠' },
        { path: '/analysis', text: '分析', icon: '📈' },
        { path: '/setting', text: '我的', icon: '👤' }
      ],
      activeTab: '/home' // 默认激活首页
    }
  },
  computed: {
    currentPath() {
      return this.$route.path
    }
  },
  watch: {
    currentPath(newPath) {
      // 检查当前路径是否在tabItems中
      const isTabPath = this.tabItems.some(item => item.path === newPath)
      if (isTabPath) {
        // 如果是tab页面，更新active状态
        this.activeTab = newPath
      }
      // 如果不是tab页面，保持当前active状态不变
    }
  },
  mounted() {
    // 初始化时检查当前路径是否在tabItems中
    const isTabPath = this.tabItems.some(item => item.path === this.currentPath)
    if (isTabPath) {
      this.activeTab = this.currentPath
    }
  },
  methods: {
    navigate(path) {
      this.$router.push(path)
    }
  }
}
</script>

<style scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #fff;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  height: 60px;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 5px 15px;
  cursor: pointer;
  flex: 1;
  height: 100%;
}

.tab-icon {
  font-size: 20px;
  filter: grayscale(1); /* 默认灰色 */
  transition: filter 0.3s ease;
}

.tab-item.active {
  background-color: rgba(64, 158, 255, 0.1); /* 选中项添加浅色背景 */
  border-radius: 8px 8px 0 0; /* 顶部圆角 */
}

.tab-item.active .tab-icon {
  filter: grayscale(0); /* 选中时正常颜色 */
}

.tab-item.active .tab-text {
  color: #409eff;
  font-weight: bold;
}

.tab-text {
  font-size: 12px;
}
</style>